package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.dto.OrderSettingDTO;
import com.itheima.health.entity.Result;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.security.annotation.MySecurity;
import com.itheima.health.security.common.RoleConst;
import com.itheima.health.service.OrderService;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.util.ExcelImportUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Role;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * @author leaflei
 * @description 预约设置控制器
 **/
@MySecurity(RoleConst.ROLE_ADMIN) //管理员权限操作
@RestController
@RequestMapping("/ordersetting")
@Slf4j
public class OrderSettingController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderSettingService OrderSettingService;

    /**
     * POI文件上传
     * 读取Excel文件
     */
    @PostMapping("/upload")
    public Result upload(@RequestParam(name = "excelFile") MultipartFile file) throws IOException {
        //读取Excel文件，获取集合
        List<OrderSetting> orderSettings = ExcelImportUtils.readFile(file.getInputStream());
        //调用业务层添加预约设置
        OrderSettingService.saveOrderSetting(orderSettings);
        //返回结果
        return new Result(true, MessageConst.IMPORT_ORDERSETTING_SUCCESS);
    }

    /**
     * 根据条件查询预约设置
     */
    @GetMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String year, String month){
        log.info("要查询的预约日期{}-{}",year,month);
        //调用业务层，查询预约设置
        List<OrderSettingDTO> orderSettingDTOList=OrderSettingService.getOrderSettingList(year,month);
        //返回结果
        return new Result(true, MessageConst.GET_ORDERSETTING_SUCCESS,orderSettingDTOList);
    }

    /**
     * 设置可预约人数
     */
    @PostMapping("/editNumberByDate")
    public Result updateNumberByDay(@RequestBody OrderSetting orderSetting){
        log.info("前端提交的参数 ===> {}",orderSetting);
        //调用业务层，修改预约设置
        OrderSettingService.updateNumberByDay(orderSetting);
        //返回结果
        return new Result(true, MessageConst.ORDERSETTING_SUCCESS);
    }

}
